<?php
App::uses('AppController', 'Controller');
/**
 * TranningPrograms Controller
 *
 * @property TranningProgram $TranningProgram
 * @property PaginatorComponent $Paginator
 */
class TranningProgramsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->TranningProgram->recursive = 0;
		$this->set('tranningPrograms', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function detail($id = null) {
		if (!$this->TranningProgram->exists($id)) {
			throw new NotFoundException(__('Invalid tranning program'));
		}
		$options = array('conditions' => array('TranningProgram.' . $this->TranningProgram->primaryKey => $id));
		$this->set('tranningProgram', $this->TranningProgram->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->TranningProgram->create();
            $files= $this->request->data['TranningProgram']['image'];
			move_uploaded_file($files['tmp_name'], WWW_ROOT.'/img/frontend/training/'.$files['name']);
			$this->request->data['TranningProgram']['create_date']= date("Y-m-d");
            $this->request->data['TranningProgram']['update_date']= date("Y-m-d");
            $this->request->data['TranningProgram']['image']=$this->webroot."img/frontend/training/".$files['name'];
			if ($this->TranningProgram->save($this->request->data)) {
				$this->Session->setFlash(__('Thêm thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}
		}
        else{
            $this->loadModel('TypeCourse');
            $typecouses = $this->TypeCourse->find('list',array("fields"=>array('TypeCourse.id','TypeCourse.type_courses_name')));
            $this->set(compact('typecouses'));
        }
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->TranningProgram->exists($id)) {
			throw new NotFoundException(__('Invalid tranning program'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		    if(!empty($this->request->data['TranningProgram']['image']['name']))
            {
                $files= $this->request->data['TranningProgram']['image'];
		        move_uploaded_file($files['tmp_name'], WWW_ROOT.'/img/frontend/training/'.$files['name']);
	        	$this->request->data['TranningProgram']['image']=$this->webroot."img/frontend/training/".$files['name'];
		  
            }
            else{
                $this->request->data['TranningProgram']['image']=$this->request->data['TranningProgram']['image_old'];
            }
		   	$this->request->data['TranningProgram']['create_date']= date("Y-m-d");
            $this->request->data['TranningProgram']['update_date']= date("Y-m-d");
            
			if ($this->TranningProgram->save($this->request->data)) {
				$this->Session->setFlash(__('Sửa thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}
		} else {
		    $this->loadModel('TypeCourse');
            $typecouses = $this->TypeCourse->find('list',array("fields"=>array('TypeCourse.id','TypeCourse.type_courses_name')));
            $this->set(compact('typecouses'));  
			$options = array('conditions' => array('TranningProgram.' . $this->TranningProgram->primaryKey => $id));
			$this->request->data = $this->TranningProgram->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->TranningProgram->id = $id;
		if (!$this->TranningProgram->exists()) {
			throw new NotFoundException(__('Invalid tranning program'));
		}
		$this->request->onlyAllow('get', 'admin_delete');
		
		if ($this->TranningProgram->delete()) {
			$this->Session->setFlash(__('Xóa thành công'),'backend/green',array('redirect'=>''));
		} else {
			$this->Session->setFlash(__('Lỗi xóa dữ liệu'),'backend/red',array('redirect'=>''));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
    public function index()
    {
        $title_for_layout= "HaPuTech-Chuong trinh dao tao";
        $trainings = $this->TranningProgram->find('all');
        $this->set(compact('trainings','title_for_layout'));
    }
   
 }
